package com.gsnd.gsnd22estore.service.impl;

import com.gsnd.gsnd22estore.dto.OrderItemDTO;
import com.gsnd.gsnd22estore.dto.QueryOrderDTO;
import com.gsnd.gsnd22estore.dto.extend.QueryOrderDTOExtend;
import com.gsnd.gsnd22estore.mapper.EsOrderMapper;
import com.gsnd.gsnd22estore.service.OrderService;
import com.gsnd.gsnd22estore.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OederServiceImpl implements OrderService {

    @Autowired
    EsOrderMapper orderMapper;
    public List<QueryOrderDTO> getOrderByUser(Integer userId, Integer status, String orderId) {
        List<QueryOrderDTO> queryOrderDTOS = orderMapper.selectByCondition(userId, status,orderId);

        // 使用数学计算
        for (QueryOrderDTO o : queryOrderDTOS) {
            // 准备两个变量进行接收
            int numSum = 0;
            double priceSum = 0.0;
            List<OrderItemDTO> orderItemsDTOS = orderMapper.selectItemByOrderId(o.getOrderId());
            for (OrderItemDTO oi : orderItemsDTOS) {
                numSum += oi.getNum();
                priceSum += oi.getTotalPrice();
            }
            o.setTotalNum(numSum);
            o.setTotalPrice(priceSum);
        }
        System.out.println(queryOrderDTOS);
        return queryOrderDTOS;
    }

    @Override
    public QueryOrderDTOExtend getOrderDatailByUser(Integer userId, String orderId) {
        List<QueryOrderDTO> queryOrderDTOS = orderMapper.selectByCondition(userId, null,orderId);
//        //复制bean对象
//        List<QueryOrderDTOExtend> queryOrderDTOExtends=BeanCopyUtils.copyBeanList(queryOrderDTOS,QueryOrderDTOExtend.class);
//        //
//        for(QueryOrderDTOExtend oe: queryOrderDTOExtends){
//            List<OrderItemsDTO> orderItemsDTOS = orderMapper.selectItemByOrderId(oe.getOrderId());
//            oe.setOrderItems(orderItemsDTOS);
        //取单个对象
        QueryOrderDTO queryOrderDTO = queryOrderDTOS.get(0);

        QueryOrderDTOExtend oe = BeanCopyUtils.copyBean(queryOrderDTOS, QueryOrderDTOExtend.class);
        List<OrderItemDTO> orderItemsDTOS = orderMapper.selectItemByOrderId(oe.getOrderId());
        oe.setOrderItems(orderItemsDTOS);

        return oe;
    }


}